package com.systop.cardecorate.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.systop.cardecorate.admin.dao.AdminDao;
import com.systop.cardecorate.admin.entity.AdminEntity;
import com.systop.cardecorate.admin.service.AdminService;
import com.systop.common.utils.PageUtils;
import com.systop.common.utils.Query;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;


@Service("adminService")
public class AdminServiceImpl extends ServiceImpl<AdminDao, AdminEntity> implements AdminService {

    @Autowired
    private AdminDao adminDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AdminEntity> page = this.page(
                new Query<AdminEntity>().getPage(params,"id",false),
                new QueryWrapper<AdminEntity>().select("id","member_name","role")
        );
        return new PageUtils(page);
    }

    @Override
    public AdminEntity queryByUserName(String memberName) {
        return adminDao.queryByUserName(memberName);
    }

    //添加用户
    @Override
    public boolean saveUser(AdminEntity admin) {
        if(this.queryByUserName(admin.getMemberName())==null){
            //sha256加密
            String salt = RandomStringUtils.randomAlphanumeric(20); //获取盐
            // System.out.println("加密前:"+admin.getMemberPass());
            admin.setMemberPass(new Sha256Hash(admin.getMemberPass(),salt).toHex());
            // System.out.println("加密后:"+admin.getMemberPass());
            admin.setSalt(salt);
            this.save(admin);
            return true;
        }
        return false;
    }

    @Override
    public boolean updateUser(AdminEntity admin){
        //修改用户信息，需要修改密码  新密码不能为空
        if(StringUtils.hasText(admin.getConfirmPass())&&admin.getConfirmPass()!=null){
            System.out.println("需要修改密码");
            return this.updateHasPass(admin);
        }else if(!StringUtils.hasText(admin.getMemberPass())){
            //修改用户信息，不需要修改密码
            System.out.println("不需要修改密码");
            admin.setConfirmPass("");
            this.updateById(admin);
            return true;
        }
        return false;
    }

    //修改用户携带密码
    public boolean updateHasPass(AdminEntity admin){
        AdminEntity queryAdmin = this.getById(admin.getId());

        //密码不正确
        if(!queryAdmin.getMemberPass().equals(
                    new Sha256Hash(admin.getMemberPass(),queryAdmin.getSalt()).toHex())){
            System.out.println("密码不正确");
            return false;
        }

        System.out.println("密码正确");
        //设置新密码替代旧密码
        admin.setMemberPass(new Sha256Hash(admin.getConfirmPass(),queryAdmin.getSalt()).toHex());

        this.updateById(admin);
        return true;
    }
}